package hanjia;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

/**
 * Description:
 * Author:    Dane
 * CreateTime:2025/2/15-下午4:22
 * Since  :   1.0
 * Version:   1.0
 */
public class LeetCode47 {



    public List<List<Integer>> permuteUnique(int[] nums) {


        Boolean[] visited = new Boolean[nums.length];
        Arrays.fill(visited,false);
        Arrays.sort(nums);
        List<Integer> result = new ArrayList<>();
        HashSet<List<Integer>> results = new HashSet<>();
        List<List<Integer>> realResult=new ArrayList<>();

        backtracking(visited, result, results, nums);
        for (List<Integer> temp:results){
            realResult.add(temp);
        }

        return realResult;
    }

    private void backtracking(Boolean[] visited, List<Integer> result, HashSet<List<Integer>> results, int[] nums) {

        if (result.size()==visited.length){

            results.add(new ArrayList<>(result));
            return;
        }



        for (int i=0;i<visited.length;i++){
            if (visited[i]){
                continue;
            }

            visited[i]=true;
            result.add(nums[i]);
            backtracking(visited,result,results,nums);
            result.remove(result.size()-1);
            visited[i]=false;



        }

    }


}
